package com.boot2523.myrabbitmq.q4;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Producer {
    private static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            // 声明一个 direct 类型的 exchange
            channel.exchangeDeclare(EXCHANGE_NAME, "direct");

            // 发送多条不同级别的日志
            sendMessage(channel, "This is an info message.", "info");
            sendMessage(channel, "This is a warning message.", "warning");
            sendMessage(channel, "This is an error message!", "error");
        }
    }

    private static void sendMessage(Channel channel, String message, String routingKey) throws Exception {
        channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
        System.out.println(" [x] Sent '" + message + "' with routing key: " + routingKey);
    }
}
